package com.qf.cx.simplenms.core.spi.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.beans.factory.annotation.Autowired;

import com.qf.cx.simplenms.core.spi.Updater;
import com.qf.cx.simplenms.persist.entity.TagMemoryEntity;
import com.qf.cx.simplenms.persist.service.api.TagMemoryService;
import com.qf.cx.simplenms.tagmemory.api.TagMemoryManager;
import com.qf.cx.simplenms.tagmemory.model.TagMemoryModel;

public class TagMemoryUpdater implements Updater {
	@Autowired
	private TagMemoryService tagMemoryService;

	@Autowired
	private TagMemoryManager tagMemoryManager;

	@Override
	public void update() {
		List<TagMemoryEntity> tagMemories = tagMemoryService.getTagMemorys();
		List<TagMemoryModel> list = new ArrayList<>();
		tagMemories.forEach(new Consumer<TagMemoryEntity>() {
			@Override
			public void accept(final TagMemoryEntity t) {
				TagMemoryModel tagMemoryModel = new TagMemoryModel(t.getName(), t.getBank(), t.getOffset(), t.getLength());
				list.add(tagMemoryModel);
			}
		});
		tagMemoryManager.updateCache(list);
	}

}
